<template>
  <div>
    <el-dialog
      :title="info.type == 'add' ? '新增菜单' : '编辑菜单'"
      :visible.sync="info.isShow"
      width="60%"
    >
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio :disabled="info.type == 'edit'" :label="1"
                >目录</el-radio
              >
              <el-radio :disabled="info.type == 'edit'" :label="2"
                >菜单</el-radio
              >
            </el-radio-group>
          </el-form-item>
          <!-- 选择菜单时才会出现下拉框 -->
          <el-form-item label="上级" prop="pid" v-show="ruleForm.type == 2">
            <el-select v-model="ruleForm.pid" placeholder="请选择上级">
              <el-option label="顶级目录" :value="0"></el-option>
              <el-option
                v-for="item in menulist"
                :key="item.id"
                :label="item.title"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标题" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
          <el-form-item label="图标" prop="icon" v-show="ruleForm.type == 1">
            <el-input v-model="ruleForm.icon"></el-input>
          </el-form-item>
          <el-form-item label="地址" prop="url" v-show="ruleForm.type == 2">
            <el-input v-model="ruleForm.url"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="ruleForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="2">禁用</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="info.isShow = false">取 消</el-button>
        <el-button type="primary" @click="info.isShow = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {menuEdit} from '@/api.js'
export default {
  props: ["info"],
  data() {
    return {
      ruleForm: {
        // 上级id，可能会重复，id不会重复
        pid: 0,
        title: "",
        icon: "",
        // 一级叫目录，二级叫菜单
        type: 1,
        url: "",
        status: 1,
      },
      menulist: [],
      rules: {
        title: [{ required: true, message: "请输入菜单标题", trigger: "blur" }],
      },
    };
  },
  created() {
    this.$http
      .get("/menulist?istree=1")
      .then((res) => {
        // console.log(res);
        this.menulist = res.list;
        // console.log(this.menulist);
      })
      .catch((err) => {
        console.log(err);
      });
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.info.type === "add") {
            this.$http
              .post("/menuadd", this.ruleForm)
              .then((res) => {
                console.log(res);
              })
              .catch((err) => {
                console.log(err);
              });
          } else {
            let res=await menuEdit(this.ruleForm)
            console.log(res);
          }
          this.info.isShow=false;
          this.$emit('update')
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    setValue(info) {
      console.log(info);
      this.ruleForm = { ...info };
    },
  },
};
</script>
<style scoped>
</style>